package models

import (
	"github.com/astaxie/beego/orm"
	"h1/utils"
)

// 获取用户信息
func UserInfo(token string) User {
	var user User
	userMod := User{}
	if claims, isValid, err := utils.ParaseToken(token); err == nil && isValid {
		_ = userMod.Query().Filter("id", claims.UId).One(&user)
		return user
	}
	userMod.Name = "无此用户"
	userMod.Email = ""
	userMod.Phone = ""
	return userMod
}

// 获取近7天的数据 3days 7days,1months,3months 6months 1year
func Get7DaysCount() map[string]interface{} {
	arr := []int{0, -1, -2, -3, -4, -5, -6}
	userMod := User{}
	sceneMod := Scene{}
	projectMod := Project{}
	mapJson := utils.MakeJson()
	for _, v := range arr {
		tempJson := utils.MakeJson()
		getDate := utils.GetDateOfNday(v)
		ucmt := userMod.GetUserPerDayCounts(getDate)
		scmt := sceneMod.GetScenePerDayCounts(getDate)
		pcmt := projectMod.GetProjectPerDayCounts(getDate)
		tempJson["user"] = ucmt
		tempJson["scene"] = scmt
		tempJson["project"] = pcmt
		mapJson[getDate] = tempJson
	}
	return mapJson
}

// 获取近7天的数据 3days 7days,30days,3months 6months 12months
func GetRandDaysCount(flag string) map[string]interface{} {
	resJson := utils.MakeJson()
	var arr []int
	var mark = "all"
	switch flag {
	case "3days":
		arr = []int{0, -1, -2}
		mark = "day"
	case "7days":
		arr = []int{0, -1, -2, -3, -4, -5, -6}
		mark = "day"
	case "30days":
		arr = []int{0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -20, -21, -22, -23, -24, -25, -25, -26, -27, -28, -29, -30}
		mark = "day"
	case "3months":
		arr = []int{-30, -60, -90}
		mark = "months"
	case "6months":
		arr = []int{-30, -60, -90, -120, -150, -180}
		mark = "months"
	case "12months":
		arr = []int{-30, -60, -90, -120, -150, -180, -210, -240, -270, -300, -330, -360}
		mark = "months"
	default:
		arr = []int{}
		mark = "all"
	}
	if mark == "day" {
		resJson = BaseDayCount(arr)
	} else if mark == "months" {
		resJson = BaseMonthsCount(arr)
	} else {
		resJson = GetAllDaysCount()
	}
	return resJson
}

func BaseDayCount(arr []int) map[string]interface{} {
	userMod := User{}
	sceneMod := Scene{}
	projectMod := Project{}
	mapJson := utils.MakeJson()
	for _, v := range arr {
		tempJson := utils.MakeJson()
		getDate := utils.GetDateOfNday(v)
		ucmt := userMod.GetUserPerDayCounts(getDate)
		scmt := sceneMod.GetScenePerDayCounts(getDate)
		pcmt := projectMod.GetProjectPerDayCounts(getDate)
		tempJson["user"] = ucmt
		tempJson["scene"] = scmt
		tempJson["project"] = pcmt
		mapJson[getDate] = tempJson
	}
	return mapJson
}

func BaseMonthsCount(arr []int) map[string]interface{} {
	userMod := User{}
	sceneMod := Scene{}
	projectMod := Project{}
	mapJson := utils.MakeJson()
	for _, v := range arr {
		tempJson := utils.MakeJson()
		old := utils.GetDateOfNday(v)
		new := utils.GetDateOfNday(v + 30)
		ucmt := userMod.GetUserRandDayCounts(old, new)
		scmt := sceneMod.GetSceneRandDayCounts(old, new)
		pcmt := projectMod.GetProjectRandDayCounts(old, new)
		tempJson["user"] = ucmt
		tempJson["scene"] = scmt
		tempJson["project"] = pcmt
		mapJson[old] = tempJson
	}
	return mapJson
}

// 获取近7天的数据
func GetAllDaysCount() map[string]interface{} {
	userMod := User{}
	sceneMod := Scene{}
	projectMod := Project{}
	tempJson := utils.MakeJson()
	ucmt := userMod.GetUserCounts()
	scmt := sceneMod.GetSceneCounts()
	pcmt := projectMod.GetProjectCounts()
	tempJson["user"] = ucmt
	tempJson["scene"] = scmt
	tempJson["project"] = pcmt
	return tempJson
}

// 数据库连表查询
func MutiTableQuery(content string, tableName string, conditions string) ([]orm.Params, error) {
	var maps []orm.Params
	var rs orm.RawSeter
	var o = orm.NewOrm()
	rs = o.Raw("select " + content + " from  " + tableName + "  where 1 > 0 and " + conditions)
	_, err := rs.Values(&maps)
	return maps, err
}

// 数据库连表查询
func MutiJoinTableQuery(content string) ([]orm.Params, error) {
	var maps []orm.Params
	var rs orm.RawSeter
	var o = orm.NewOrm()
	rs = o.Raw(content)
	_, err := rs.Values(&maps)
	return maps, err
}
